package cn.myzqu.sign.message.server.service.impl;


import cn.myzqu.sign.message.server.base.page.Page;
import cn.myzqu.sign.message.server.dao.OsMessageMapper;
import cn.myzqu.sign.message.server.entity.OsMessage;
import cn.myzqu.sign.message.server.service.MessageService;
import cn.myzqu.sign.message.server.util.KeyUtil;
import cn.myzqu.sign.message.server.vo.GroupMessageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统通知服务实现类
 *
 * @Author: 小柳
 * @Date: 2019/1/24 15:24
 */
@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Autowired
    private OsMessageMapper osMessageMapper;

    /**
     * 新增系统通知消息实现方法
     *
     * @param osMessage
     * @return
     */
    @Override
    public Boolean addMessage(OsMessage osMessage) {
        osMessage.setCode(KeyUtil.genUniqueKey());
        osMessage.setCreateTime(new Date());
        osMessage.setStatus((byte) 1);
        osMessage.setReadStatus((byte) 0);
        int result = osMessageMapper.insert(osMessage);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public OsMessage selectById(Integer id) {
        return osMessageMapper.selectById(id);
    }

    @Override
    public Boolean addSysMessage(String receiverId, String theme, String content) {
        OsMessage osMessage = new OsMessage();
        osMessage.setSenderId(10000);
        osMessage.setSenderName("系统消息");
        osMessage.setTheme(theme);
        osMessage.setContent(content);
        osMessage.setReceiverId(Integer.parseInt(receiverId));

        osMessage.setCode(KeyUtil.genUniqueKey());
        osMessage.setCreateTime(new Date());
        osMessage.setStatus((byte) 1);
        osMessage.setReadStatus((byte) 0);
        int result = osMessageMapper.insert(osMessage);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据消息发送者分组，查询最新消息
     * @param userId
     * @return
     */
    @Override
    public List<GroupMessageVO> selectUserMessageGroupBySender(Integer userId) {
        return osMessageMapper.selectUserMessageGroupBySender(null,userId);
    }
    @Override
    public Page<GroupMessageVO> selectUserMessageGroupBySender(Integer userId , Integer current, Integer size) {
        Page<GroupMessageVO> page = new Page<>(current,size);
        page.setRecords(osMessageMapper.selectUserMessageGroupBySender(page, userId));
        return page;
    }

    @Override
    public Page<OsMessage> selectUserMessageBySender(Integer userId, Integer senderId , Integer current, Integer size) {
        Page<OsMessage> page = new Page<>(current,size);
        QueryWrapper<OsMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("receiver_id",userId)
                .eq("sender_id",senderId)
                .orderByDesc("create_time");
        osMessageMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 修改系统通知已读状态（0：未读；1：已读
     *
     * @param id
     * @return
     */
    @Override
    public Boolean changeReadStatus(Integer id) {
        Byte readStatus = (byte) 1;
        OsMessage osMessage = new OsMessage();
        osMessage.setId(id);
        osMessage.setReadStatus(readStatus);
        if (osMessageMapper.updateById(osMessage) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改系统通知可见状态
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public Boolean changeStatus(Byte status, Integer id) {
        OsMessage osMessage = new OsMessage();
        osMessage.setId(id);
        osMessage.setReadStatus(status);
        int result = osMessageMapper.updateById(osMessage);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据用户id分页查找出系统通知
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page findByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        Page<OsMessage> page = new Page<>(pageNum,pageSize);
        QueryWrapper<OsMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id",userId);
        osMessageMapper.selectPage(page,queryWrapper);
        return page;
    }



}
